package cn.jiyun.controller;

import cn.jiyun.pojo.Huiyuan;
import cn.jiyun.service.impl.Uservice;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private Uservice uservice;

    @PostMapping("qc")
    public Page<Huiyuan> qc(Integer page,Integer pagesize,String name){
        Page<Huiyuan> page1=new Page<>(page,pagesize);
        return uservice.qc(page1,name);

    }

    @RequestMapping("daoru")
    public void daoru(MultipartFile excelFile) throws IOException {
        Workbook workbook=new XSSFWorkbook(excelFile.getInputStream());
        Sheet sheet=workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            Row row=sheet.getRow(i);
            Integer bh=(int)row.getCell(0).getNumericCellValue();
            String name=row.getCell(1).getStringCellValue();
            String sex=row.getCell(2).getStringCellValue();
            Integer sfzh=(int)row.getCell(3).getNumericCellValue();
            Integer sjh=(int)row.getCell(4).getNumericCellValue();
            Integer yx=(int)row.getCell(5).getNumericCellValue();
            Huiyuan huiyuan=new Huiyuan();
            huiyuan.setBh(bh.toString());
            huiyuan.setName(name);
            huiyuan.setSex(sex);
            huiyuan.setSfzh(sfzh.toString());
            huiyuan.setSjh(sjh.toString());
            huiyuan.setYx(yx.toString());
            uservice.add(huiyuan);
        }

    }

    @PostMapping("update")
    public void update(@RequestBody Huiyuan huiyuan){
        uservice.update(huiyuan);
    }

    @PostMapping("add")
    public void add(@RequestBody Huiyuan huiyuan){
         uservice.add(huiyuan);
    }
    @GetMapping("del")
    public void del(Integer id){
            uservice.del(id);
    }

    @GetMapping("dc")
    public void dc() throws IOException {
        Workbook workbook=new XSSFWorkbook();
        Sheet sheet=workbook.createSheet("信息");
        Row row=sheet.createRow(0);
        String []tou={"名字","性别","邮箱","电话","图片","时间","部门"};
        for (int i = 0; i < tou.length; i++) {
            row.createCell(i).setCellValue(tou[i]);
        }
        List<Huiyuan> dc = uservice.dc();
        for (int i = 0; i < dc.size(); i++) {
           Huiyuan huiyuan=dc.get(i);
           Row row1=sheet.createRow(i+1);
           row.createCell(0).setCellValue(huiyuan.getId());
            row1.createCell(1).setCellValue(huiyuan.getName());
            row1.createCell(2).setCellValue(huiyuan.getSex());
            row1.createCell(3).setCellValue(huiyuan.getSfzh());
            row1.createCell(4).setCellValue(huiyuan.getSjh());
            row1.createCell(5).setCellValue(huiyuan.getYx());
        }
        FileOutputStream fo=new FileOutputStream("E:/"+"dc.xlsx");
        workbook.write(fo);
        fo.close();

    }

    @GetMapping("hx")
    public Huiyuan hx(Integer id){
     return    uservice.hx(id);
    }
}
